package aom.ju.ss.view.shua

import android.os.Message
import android.view.View
import androidx.databinding.ViewDataBinding
import aom.ju.ss.R
import aom.ju.ss.databinding.AttentionItemBinding
import aom.ju.ss.lpInterface.TitleDialogInterface
import aom.ju.ss.mode.LpKey.LOCATE_ERR
import aom.ju.ss.mode.LpKey.LOCATE_OK
import aom.ju.ss.mode.LpKey.TO_LOCATE
import aom.ju.ss.mode.Shop
import aom.ju.ss.mode.nearbyShops
import aom.ju.ss.utils.*
import aom.ju.ss.utils.http.LP_API.nearby_list
import aom.ju.ss.view.base.BaseListFragment
import aom.ju.ss.view.dialog.TitleDialog
import com.bumptech.glide.Glide
import com.google.gson.Gson

/**
 * 刷店->附近的店铺
 */
class NearbyShopsFragment : BaseListFragment() {

    init {
        isShow = false
    }

    companion object {
        fun newInstance(): NearbyShopsFragment {
            return NearbyShopsFragment()
        }
    }

    override fun api(): String {
        return nearby_list
    }

    override fun key(): Array<String> {
        return arrayOf(
            "lng",
            "lat",
            "page"
        )
    }

    override fun value(): Array<String> {
        return arrayOf(
            SpUtils.getString("lng"),
            SpUtils.getString("lat"),
            page.toString()
        )
    }

    override fun item(): Int {
        return R.layout.attention_item
    }

    override fun bind(b: ViewDataBinding, position: Int) {
        super.bind(b, position)
        val ib = b as AttentionItemBinding
        val info = getList()[position] as Shop
        ib.info = info

        if (info.logo.contains(".gif")) {
            Glide.with(this)
                .asGif()
                .load(info.logo)
                .error(R.drawable.img_dp)
                .into(ib.appCompatImageView8)
        } else {
            Glide.with(this)
                .load(info.logo)
                .error(R.drawable.img_dp)
                .into(ib.appCompatImageView8)
        }

        ib.rootView.setOnClickListener {
            start(ShopDetailsActivity::class.java, info.id.toString())
        }
    }

    override fun ok(jsonString: String, httpTY: String) {
        super.ok(jsonString, httpTY)
        val mode = Gson().fromJson(jsonString, nearbyShops::class.java)
        setList(mode.data)
    }

    override fun error(e: String?, jsonString: String?, httpTY: String?) {
        super.error(e, jsonString, httpTY)
        e?.show()
    }

    override fun onBaseCreateView() {
        super.onBaseCreateView()
        binding?.rootView?.setBackgroundResource(R.color.color_f5f5f5)
    }

    override fun callBack(event: Message) {
        super.callBack(event)
        if (event.arg1 == LOCATE_OK) {
            isHttp = true
            data
        } else if (event.arg1 == LOCATE_ERR) {
            TitleDialog()
                .show(activity, object : TitleDialogInterface {
                    override fun ok(view: View?, IsDetermine: Boolean) {
                        if (IsDetermine) {
                            SendMsg.sendEvent(TO_LOCATE)
                        }
                    }
                }).setConnect("需要定位数据以获取数据,是否同意定位")
                .tvCancel("取消")
                .tvDetermine("同意")

        }
    }
}
